package zjl.util;

import java.util.List;

public class Utility {

    private static int gcd(int a, int b) {
        while (b > 0) {
            int temp = b;
            b = a % b;
            a = temp;
        }
        return a;
    }

    private static int lcm(int a, int b) {
        return a * (b / gcd(a, b));
    }

    // 求序列的最大公约数
    public static int GCD(List<Integer> data) {
        if (data == null || data.isEmpty()) {
            return -1;
        }

        int result = data.get(0);
        for (int i = 1; i < data.size(); i++) {
            result = gcd(result, data.get(i));
        }

        return result;
    }

    // 求序列的最小公倍数
    public static int LCM(List<Integer> data) {
        if (data == null || data.isEmpty()) {
            return -1;
        }

        int result = data.get(0);
        for (int i = 1; i < data.size(); i++) {
            result = lcm(result, data.get(i));
        }

        return result;
    }

    public static int upmod(double divison, double divisor) {
        double dval = divison / divisor;
        int ival = (int)dval;

        if (dval > ival)
            return ival + 1;

        return ival;
    }
}
